<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="excel">excel导入</el-button>
          <el-button type="danger" size="small" @click="exporExcel">excel导出</el-button>
          <el-button type="primary" size="small" @click="addEmployees">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template slot-scope="scope">
              <el-avatar :size="60" :src="scope.row.staffPhoto">
                <img id="curImg" src="@/assets/common/bigUserHeader.png">
              </el-avatar>
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template slot-scope="scope">
              {{ formatEmployment(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="账户状态" prop="workNumber" />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push('/employees/detail?id='+scope.row.id)">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="pageObj.page"
            :page-sizes="[1, 2, 3, 5]"
            :page-size="pageObj.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog
        title="新增弹层"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <empDialog v-if="showDialog" @close="close" @update-empolyee="hUpdateEmpolyee" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getUserList } from '@/api/employees'
import EmployeeEnum from '@/constant/employees'
const hireType = EmployeeEnum.hireType.reduce((acc, item) => {
  acc[item.id] = item.value
  return acc
}, {})
export default {
  data() {
    return {
      list: [], // 员工信息
      pageObj: {
        page: 1,
        size: 5
      }, // 查询的页数
      total: 1, // 一共多少条
      showDialog: false // 控制弹框
    }
  },
  created() {
    this.loadUserList()
  },
  methods: {

    async loadUserList() {
      try {
        const res = await getUserList(this.pageObj)
        this.list = res.data.rows
        this.total = res.data.total
        console.log(res)
      } catch (err) {
        console.log(err)
      }
    }, // 获取员工信息
    formatEmployment(num) {
      return hireType[num]
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageObj.size = val
      this.loadUserList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageObj.page = val
      this.loadUserList()
    },
    addEmployees() {
      this.showDialog = true
    }, // 新增
    close() {
      this.showDialog = false
    },
    // 收到子组件抛出的添加成功的事件
    hUpdateEmpolyee() {
      this.showDialog = false
      this.pageObj.page = Math.ceil(++this.total / this.pageObj.size)
      this.loadUserList()
    },
    excel() {
      this.$router.push('/employees/import')
    },
    async transExcer() {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      } // 枚举
      const hireTypEnmu = { 1: '正式', '2': '非正式' } // 枚举
      const res = await getUserList(this.pageObj)
      console.log('excel转换需要的数据', res)
      const enKeys = Object.keys(res.data.rows[0]) // 得到英文表头
      const header = enKeys.map(item => map[item]) // 得到要导出中文表头
      const data = res.data.rows.map(item => {
        item.formOfEmployment = hireTypEnmu[item.formOfEmployment]
        return Object.values(item)
      }) // 得到要导出的具体数据
      console.log(header, data)
      return { header, data }
    }, // 转换导出需要的数据
    async exporExcel() {
      const { header, data } = await this.transExcer()// 转换excel数据
      import('@/components/exportExcel/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
      })
    }, // 导出excel
    Fn(a) {
      console.log(a)
    }
  }
}
</script>
<style>
  .el-avatar img{
    width: 100%;
    height: 100%;
  }
</style>
